package net.oschina.ivansong.maven.plugin;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;

@Mojo(name = "countJava", defaultPhase = LifecyclePhase.PREPARE_PACKAGE)
public class CountJavaMojo extends AbstractMojo {

	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {
		getLog().info("---- countJava Plugin------------------------ ");
		
		File targetDir = new File("src/main/java");
		
		if (!targetDir.exists()) {
			getLog().info("---- fail count------------------------");
			return;
		}
		List<File> javaFiles = new ArrayList<>();
		collectJavaFiles(javaFiles, targetDir);
		
		getLog().info("---- java file count:" + javaFiles.size() + "------------------------");

	}
	
	private void collectJavaFiles(List<File> collecte, File file) {
		if (file.isFile()) {
			if (file.getName().endsWith(".java")) {
				collecte.add(file);
			}
		} else {
			for (File file2 : file.listFiles()) {
				collectJavaFiles(collecte, file2);
			}
		}
		
	}

}
